package com.lzx.iteam.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.lzx.iteam.EventDetailContentActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.EventVoteBean;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;

/**
 * Created by zhangshuai on 15/10/12.
 */
public class VoteDetailAdapter extends BaseAdapter {


    private Context mContext;
    private ArrayList<EventVoteBean> mData;
    private int mCount;

    public VoteDetailAdapter(Context mContext,ArrayList<EventVoteBean> mData,int mCount){
        this.mContext = mContext;
        this.mData = mData;
        this.mCount = mCount;

    }

    public void bindData(ArrayList<EventVoteBean> mData,int mCount){
        this.mData = mData;
        this.mCount = mCount;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            holder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.vote_item_detail_layout,null);
            holder.mTitle = (TextView) view.findViewById(R.id.vote_detail_item_title);
            holder.mCount = (TextView) view.findViewById(R.id.vote_detail_item_count);
            holder.mProgress = (ProgressBar) view.findViewById(R.id.vote_detail_item_progress);
            view.setTag(holder);

        }else{
            holder = (ViewHolder) view.getTag();
        }
        /*RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) holder.mView.getLayoutParams();
        linearParams.width = 200;
        holder.mView.setBackgroundColor(Color.parseColor("#ff0000"));
        holder.mView.setLayoutParams(linearParams);*/
        holder.mTitle.setText(mData.get(i).getVote());
        /*if (EventDetailContentActivity.mVote != null && ((char)(65 + i)+"").equals(EventDetailContentActivity.mVote)){
            holder.mProgress.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.progress_bg_item2));
        }else{
            holder.mProgress.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.progress_bg_item1));
        }*/
        holder.mProgress.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.progress_bg_item1));
        if (!StringUtil.isEmpty(EventDetailContentActivity.mVote)){
            if (EventDetailContentActivity.mVote.length() > 1){
                String[] mVotes = EventDetailContentActivity.mVote.split(",");
                for (int j = 0; j < mVotes.length; j++) {
                    if (((char)(65 + i)+"").equals(mVotes[j])){
                        holder.mProgress.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.progress_bg_item2));
                        break;
                    }
                }
            }else if (((char)(65 + i)+"").equals(EventDetailContentActivity.mVote)){
                holder.mProgress.setProgressDrawable(mContext.getResources().getDrawable(R.drawable.progress_bg_item2));
            }
        }

        int progress = 0;
        if (mCount != 0){
            progress = mData.get(i).getCount()*100/mCount;
        }
        holder.mCount.setText(mData.get(i).getCount()+" ("+progress+"%)");
        holder.mProgress.setProgress(progress);

        return view;
    }

    class ViewHolder{
        TextView mTitle;
        ProgressBar mProgress;
        TextView mCount;
    }

}
